package com.cll.controller.admin;

import com.cll.constant.JwtClaimsConstant;
import com.cll.dto.EmployeeDTO;
import com.cll.dto.EmployeeLoginDTO;
import com.cll.dto.EmployeePageQueryDTO;
import com.cll.entity.Employee;
import com.cll.properties.JwtProperties;
import com.cll.result.PageResult;
import com.cll.result.Result;
import com.cll.service.EmployeeService;
import com.cll.utils.JwtUtil;
import com.cll.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("登入")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO, HttpSession session) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //测试Thread  后端tomcat服务器是不是都会去分配员工单独的线程来处理请求
        // 登录被排除了,一次就只是同一个线程  一个请求是会走控制层和业务层的 所以可以在这两个地方测

        log.info("当前线程id:{}",Thread.currentThread().getId());

		//将登录数据员工id存储到session会话中
        //session.setAttribute("employee",employee.getId());

        //生成令牌
        Map<String, Object> claims = new HashMap<>();
        //将员工的id写入载荷
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        String token = JwtUtil.createJWT(
                //获取密钥
                jwtProperties.getAdminSecretKey(),
                //获取有效时间
                jwtProperties.getAdminTtl(),
                //载荷
                claims
        );

        //由于EmployeeLoginVO  使用@Builder注解  所以可以使用链式构建对象
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        log.info("令牌{}",token);

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("登出")
    @PostMapping("/logout")
    public Result<String> logout() {

        return Result.success();
    }


    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping  //前端传过来的EmployeeDTO是前端封装好的json数据,要用@RequestBody来接收(用来实现接收http请求的json数据，将json数据转换为java对象)
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        //打印日志  把接收的数据打印出来,便于看是否正确
        log.info("新增员工: {}",employeeDTO);
        //调用业务处理逻辑方法
       employeeService.save(employeeDTO);
        //返回数据 Result类定义了后端统一返回结果格式
        return Result.success("成功增加员工");
    }

    /**
     * 员工分类查询
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        //打印请求数据日志
        log.info("员工分页查询:{}",employeePageQueryDTO);

        //调用业务获取分页数据
        PageResult pageResult = employeeService.page(employeePageQueryDTO);

        //返回数据
        return Result.success(pageResult);
    }

    /**
     * 启用或禁用员工状态
     * @param status
     * @param id
     * @return
     */

    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用员工状态")
    public Result startOrStop(@PathVariable Integer status, Long id){

        log.info("启用或禁用员工状态:status={},id={}",status,id);

        employeeService.startOrStop(status,id);

        return Result.success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<Employee> findById(@PathVariable Long id){

        log.info("根据id查询员工:{}",id);
        Employee employee = employeeService.findById(id);
        return Result.success(employee);
    }


    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改员工信息:{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
